//
//  HttpRequest.swift
//  daily_count
//
//  Created by 段帅 on 2018/3/28.
//  Copyright © 2018年 Sooc. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestType:String {
    case GET = "get"
    case POST = "post"
}

class HttpRequest: AFHTTPSessionManager {
   
    
     static let _shareInstance = HttpRequest()
    class func shareManager()->HttpRequest{
       
        _shareInstance.responseSerializer = AFHTTPResponseSerializer();
        return _shareInstance;
    }
        
   
    
    /// 封装网络请求方法
    ///
    /// - Parameters:
    /// - Method: GET/POST, 默认是GET请求
    /// - URLString: 请求地址
    /// - parameters: 参数
    /// - completed: 结束回调
     public  func request(Method:RequestType = .GET, URLString: String,parameters: [String: AnyObject]?, completed:@escaping ((_ json: AnyObject?, _ isSuccess: Bool)->())) {
        
        /// 定义成功回调闭包
        let success = { (task: URLSessionDataTask,json: Any?)->() in
            do {
                let data = try JSONSerialization.jsonObject(with: json as! Data, options: JSONSerialization.ReadingOptions.allowFragments) as! [String:AnyObject];
                if (data["status"] as! Int == 0) {
                    completed(data as AnyObject,true)
                }else{
                    completed(data as AnyObject,false)
                }
                
                
            }catch let error as NSError {
                print(error);
                completed(["status":1,"statusCode":"json解析失败"] as AnyObject,false)
            }
          
        }   
        
        /// 定义失败回调闭包
        let failure = {(task: URLSessionDataTask?, error: Error)->() in
            print(error);
            completed(["status":1,"statusCode":"请求失败"] as AnyObject,false)
        }
        
        /// 通过请求方法,执行不同的请求
        // 如果是 GET 请求
        if Method == .GET { // GET
            
            HttpRequest.shareManager().get(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
            
        } else { // POST
           HttpRequest.shareManager().post(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
            
        }
    }
    
    
    
    
    public  func request2(URLString: String,image:UIImage,parameters: [String: AnyObject]?, completed:@escaping ((_ json: AnyObject?, _ isSuccess: Bool)->())) {
        
        /// 定义成功回调闭包
        let success = { (task: URLSessionDataTask,json: Any?)->() in
            do {
                let data = try JSONSerialization.jsonObject(with: json as! Data, options: JSONSerialization.ReadingOptions.allowFragments) as! [String:AnyObject];
                if (data["status"] as! Int == 0) {
                    completed(data as AnyObject,true)
                }else{
                    completed(data as AnyObject,false)
                }
                
                
            }catch let error as NSError {
                print(error);
                completed(["status":1,"statusCode":"json解析失败"] as AnyObject,false)
            }
            
        }   
        
        /// 定义失败回调闭包
        let failure = {(task: URLSessionDataTask?, error: Error)->() in
            print(error);
            completed(["status":1,"statusCode":"请求失败"] as AnyObject,false)
        }
        

        HttpRequest.shareManager().post(URLString, parameters: parameters, constructingBodyWith: { (formData:AFMultipartFormData) in
//            let imageData = UIImagePNGRepresentation(image);
            let imageData = UIImageJPEGRepresentation(image,0.2);
            formData.appendPart(withFileData: imageData!, name:"image", fileName:"file.jpg", mimeType: "image/jpeg")
        }, progress: { (process) in
            
        }, success: success, failure: failure);
            
        
    }
    

}


